package com.example.lenovo.ruidaoproject;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.lenovo.ruidaoproject.fragment.HotCoursesFragment;
import com.example.lenovo.ruidaoproject.fragment.PostCoursesFragment;
import com.example.lenovo.ruidaoproject.fragment.TeamFragment;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;
    private ArrayList<Fragment> fragmentArrayList;
    private HotCoursesFragment hotCrousesFragment;
    private PostCoursesFragment postCoursesFragment;
    private TeamFragment teamFragment;
    RadioGroup radioGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup=(RadioGroup)findViewById(R.id.radio);
        viewPager=(ViewPager)findViewById(R.id.pager);
        viewPager.addOnPageChangeListener(new onPagerChange());
        radioGroup.setOnCheckedChangeListener(new onCheckerChange());
        fragmentArrayList=new ArrayList<>();
        hotCrousesFragment=new HotCoursesFragment();
        postCoursesFragment=new PostCoursesFragment();
        teamFragment=new TeamFragment();
        fragmentArrayList.add(postCoursesFragment);
        fragmentArrayList.add(hotCrousesFragment);
        fragmentArrayList.add(teamFragment);

    }
    class MyFragmentPagetAdapter extends FragmentPagerAdapter{
        public MyFragmentPagetAdapter(FragmentManager fragmentManager){
            super(fragmentManager);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentArrayList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentArrayList.size();
        }
    }
    class onCheckerChange implements RadioGroup.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int current =0;
            switch (checkedId){
                case R.id.postCourses:
                    current=0;
                    break;
                case R.id.hotCourses:
                    current=1;
                    break;
                case R.id.team:
                    current=2;
                    break;
            }
            if(viewPager.getCurrentItem()!=current){
                viewPager.setCurrentItem(current);
            }
        }
    }
    class onPagerChange implements ViewPager.OnPageChangeListener{
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            int current=viewPager.getCurrentItem();
            switch (current){
                case 0:
                    radioGroup.check(R.id.postCourses);
                    break;
                case 1:
                    radioGroup.check(R.id.hotCourses);
                    break;
                case 2:
                    radioGroup.check(R.id.team);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
